<?php
namespace Admin\Controller;

/**
 * FieldsController
 * 字段管理
 */
class FieldsController extends CommonController {
    private $model;
    private $service;
    private $fieldtype;


    public function _initialize() {
        parent::_initialize();
        $this->model = D("Field");
        $this->service = D("Field", "Service");
        $this->fieldtype = $this->model->fieldtype;
        $this->assign('fieldtype', $this->fieldtype);
    }

    /**
     * 添加字段
     * @return
     */
    public function add() {
        $model_id = I("get.model_id", 0, "intval");
        if ($model_id <= 0) {
            return $this->error('您需要添加字段的模型不存在！');
        }
        
        $model = M('Model')->getById($model_id);
        $mainTableFields = M("Content")->getDbFields();
        $addTableFields = M($model['addtable'])->getDbFields();
        
        $fields = array_merge($mainTableFields, $addTableFields);
        
        if (!isArrayAndNotEmpty($model)) {
            return $this->error('您需要添加字段的模型不存在！');
        }
        $f = '';
        foreach($fields as $k=>$v)
        {
            $f .= ($f=='' ? $v : ' '.$v);
        }
        $this->assign('f', $f);
        $this->assign('model', $model);
        $this->assign('mid', $model_id);
        $this->display();
    }

    /**
     * 创建字段
     * @return
     */
    public function create() {
        $field = I("post.field");
        if (!IS_POST || !isArrayAndNotEmpty($field)) {
            return $this->errorReturn('无效的操作！');
        }
        // field
        $result = $this->service->checkField($field);
        if (!$result['status']) {
            return $this->errorReturn($result['data']['error']);
        }
        if (!D('Model', 'Service')->existModel($field['model_id'])) {
            return $this->error('您需要添加字段的模型不存在！');
        }
        
        // 插入field
        $result = $this->service->add($field);
        if (!$result['status']) {
            return $this->errorReturn('系统出错了！');
        }

        $url = U('Models/show', array('id' => $field['model_id']));
        return $this->successReturn('字段添加成功！', $url);
    }

    /**
     * 编辑字段
     * @return
     */
    public function edit() {
        $model_id = I("get.model_id", 0, "intval");
        $field_id = I("get.field_id", 0, "intval");
        if ($model_id <= 0
            || $field_id <= 0
            || !D('Model', 'Service')->existModel($model_id)
            || !D('Field', 'Service')->existField($field_id)) {
            return $this->error('您需要编辑的字段不存在！');
        }

        $model = M('Model')->getById($model_id);
        $field = D('Field')->relation(true)->getById($field_id);
        $models = D('Model', 'Service')->getAll();

        $this->assign('models', $models);
        $this->assign('model', $model);
        $this->assign('field', $field);
        $this->display();
    }

    /**
     * 更新字段
     * @return
     */
    public function update() {
        $field = I("post.field");
        if (!IS_POST || !isArrayAndNotEmpty($field)) {
            return $this->errorReturn('无效的操作！');
        }

        if (!D('Model', 'Service')->existModel($field['model_id'])) {
            return $this->error('您需要修改字段的模型不存在！');
        }

        if (!$this->service->existField($field['id'])) {
            return $this->error('您需要修改的字段不存在！');
        }
        // field
        $result = $this->service->checkField($field, $field['id']);
        if (!$result['status']) {
            return $this->errorReturn($result['data']['error']);
        }


        // 更新field
        $result = $this->service->update($field);
        if (!$result['status']) {
            return $this->errorReturn('系统出错了！');
        }

        $url = U('Models/show', array('id' => $field['model_id']));
        return $this->successReturn('字段更新成功！', $url);
    }

    /**
     * 删除字段
     * @return
     */
    public function delete() {
        $model_id = I("get.model_id", 0, "intval");
        $field_id = I("get.field_id", 0, "intval");
        if ($model_id <= 0) {
            return $this->errorReturn('需要删除字段所在的模型不存在！');
        }

        if ($field_id <= 0) {
            return $this->errorReturn('需要删除的字段不存在！');
        }

        if (!D('Model', 'Service')->hasField($model_id, $field_id)) {
            return $this->errorReturn('无效的操作！');
        }

        $result = D('Field', 'Service')->delete($field_id);
        if (!$result['status']) {
            return $this->errorReturn('系统出错了！');
        }

        return $this->successReturn('删除字段成功！');
    }
}
